package com.eagleframework.api;

import com.eagle.util.log.LogWriter;
import com.eagle.util.text.StringUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.io.ResourceLoader;

/**
 * 应用启动器
 *
 * @author yaojx
 */
public class EagleApplicationRunner extends SpringApplication {
    /**
     * 默认应用名称
     */
    private static final String DEFAULT_APP_NAME = "EagleApp";
    /**
     * 应用名称
     */
    private String appName;
    
    public EagleApplicationRunner(Class<?>... primarySources) {
        super(primarySources);
    }
    
    public EagleApplicationRunner(String appName, Class<?>... primarySources) {
        super(primarySources);
        this.appName = appName;
    }
    
    public EagleApplicationRunner(ResourceLoader resourceLoader, Class<?>... primarySources) {
        super(resourceLoader, primarySources);
    }
    
    public EagleApplicationRunner(String appName, ResourceLoader resourceLoader, Class<?>... primarySources) {
        super(resourceLoader, primarySources);
        this.appName = appName;
    }
    
    @Override
    public ConfigurableApplicationContext run(String... args) {
        ConfigurableApplicationContext springApplicationRun = super.run(args);
        LogWriter.info("******************************************");
        LogWriter.info("        {}--启动完成", StringUtil.nullToDefault(this.appName, DEFAULT_APP_NAME));
        LogWriter.info("******************************************");
        return springApplicationRun;
    }
    
    /**
     * @param appName 
     * @param args
     * @return
     */
    public ConfigurableApplicationContext run(String appName, String... args) {
        ConfigurableApplicationContext springApplicationRun = super.run(args);
        LogWriter.info("******************************************");
        LogWriter.info("        {}--启动完成", StringUtil.nullToDefault(appName, DEFAULT_APP_NAME));
        LogWriter.info("******************************************");
        return springApplicationRun;
    }
    
    /**
     * @param primarySource 
     * @param args
     * @return
     */
    public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
        ConfigurableApplicationContext run = SpringApplication.run(new Class<?>[]{primarySource}, args);
        LogWriter.info("******************************************");
        LogWriter.info("        {}--启动完成", DEFAULT_APP_NAME);
        LogWriter.info("******************************************");
        return run;
    }
    
    /**
     * @param appName 
     * @param primarySource
     * @param args
     * @return
     */
    public static ConfigurableApplicationContext run(String appName, Class<?> primarySource, String... args) {
        ConfigurableApplicationContext run = SpringApplication.run(new Class<?>[]{primarySource}, args);
        LogWriter.info("******************************************");
        LogWriter.info("        {}--启动完成", StringUtil.nullToDefault(appName, DEFAULT_APP_NAME));
        LogWriter.info("******************************************");
        return run;
    }
}
